package com.fan.analysis.module.preprocessing.fusion.service;

import com.fan.analysis.module.preprocessing.fusion.dao.FusionDao;
import com.fan.analysis.module.preprocessing.fusion.domain.bo.CallCountBo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 异步处理方法
 *
 * @author fan
 * @date 2021/09/20 12:56
 **/
@Service
public class AsyncService {

    @Autowired
    private FusionDao fusionDao;

    @Async
    @Transactional(rollbackFor = Exception.class)
    public void asyncCallFusion(String phoneNum, List<CallCountBo> callCountBoCallingNum, List<CallCountBo> callCountBoCalledNum) {
        callCountBoCallingNum.addAll(callCountBoCalledNum);

        // 遍历更新
        for (CallCountBo callCountBo : callCountBoCallingNum) {
            fusionDao.updateCallNumber(callCountBo.getPhoneNum1(), callCountBo.getPhoneNum2(), callCountBo.getCallCount());
            fusionDao.updateCallNumber(callCountBo.getPhoneNum2(), callCountBo.getPhoneNum1(), callCountBo.getCallCount());


            // 返回 电话1 电话2 常用通话小时 次数
            CallCountBo callCountBo1 = fusionDao.commonContactTimePeriod(callCountBo.getPhoneNum1(), callCountBo.getPhoneNum2());
            String sTime = callCountBo1.getContactTime();
            String eTime = Integer.parseInt(callCountBo1.getContactTime()) + 1 + "";
            if ("24".equals(sTime)) {
                eTime = "1";
            }
            String contactTime = "[" + sTime + "," + eTime + "]";
            fusionDao.updateContactTime(callCountBo1.getPhoneNum1(), callCountBo1.getPhoneNum2(), contactTime);
            fusionDao.updateContactTime(callCountBo1.getPhoneNum2(), callCountBo1.getPhoneNum1(), contactTime);

        }
        fusionDao.updateFlag(1, phoneNum);
    }

    @Async
    @Transactional(rollbackFor = Exception.class)
    public void asyncMessageFusion(String phoneNum, List<CallCountBo> countBySendNum, List<CallCountBo> countByRecNum) {
        countBySendNum.addAll(countByRecNum);

        for (CallCountBo callCountBo : countBySendNum) {
            fusionDao.updateMessageNumber(callCountBo.getPhoneNum1(), callCountBo.getPhoneNum2(), callCountBo.getMessageCount());
            fusionDao.updateMessageNumber(callCountBo.getPhoneNum2(), callCountBo.getPhoneNum1(), callCountBo.getMessageCount());
        }
        fusionDao.updateMessageFlag(1, phoneNum);
    }
}
